using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NPCController : MonoBehaviour
{
    public List<GameObject> documents;
    public int documentCount;

    public Sprite NPCTemperature;

    public GameObject rightMask;
    public GameObject wrongMask;
    public GameObject dialogBox;
    public bool isWearMaskCorrectly;
    public bool isHaveMask;

    public List<GameObject> documentsOnTheTable;

    public Sprite phoneMagnified;

    public GameObject phoneAskModeClickablePart;

    public List<GameObject> DialogBox;

    public GameObject Arm;

    public bool CanAccess;
    public bool IsCarryCOVID;
    

    private void Start()
    {

        documentCount = documents.Count;
        //transform.localPosition = new Vector3(0, -21.6f, 0);
    }

    // NPC入场后，需生成对应的文件
    public void NPCGenerateDocuments()
    {
        foreach (var document in documents)
        {
            documentsOnTheTable.Add(DocumentsManager.instance.GenerateDocument(document));
        }
    }

    public void GiveBackDocument()
    {
        documentCount--;
        if (documentCount == 0)
        {
            NPCManager.instance.NPCApprove();
        }
    }

    public void NPCLeave()
    {
        Debug.Log("NPC Leave");
        NPCManager.instance.CheckedNPCNumber++;
        if (UIManager.instance.isTimeToGoHome)
        {
            UIManager.instance.WhenEnd();
        }
        else
        {
            NPCManager.instance.GenerateNPC();
        }

        Destroy(gameObject);
    }

    public void DestroyNPCDocument()
    {
        foreach (var document in documentsOnTheTable)
        {
            Destroy(document);
        }
    }

    public void OnMouseEnter()
    {
        if (NPCManager.instance.isInAskMode)
        {
            Cursor.SetCursor(NPCManager.instance.cursorTextureMagnifier, Vector2.zero, CursorMode.ForceSoftware);
        }

    }

    public void OnMouseExit()
    {
        Cursor.SetCursor(NPCManager.instance.cursorTextureNormal, Vector2.zero, CursorMode.ForceSoftware);
    }

    public void OnMouseDown()
    {
        //条件1：是否处于闻讯模式下
        if (NPCManager.instance.isInAskMode)
        {
            //条件2:是否佩戴好口罩
            if (isWearMaskCorrectly)
            {
                //没问题
                UIManager.instance.MessageBox.SetActive(true);
                Invoke("CloseMessageBox", 1.2f);
            }
            else
            {
                //条件3：有没有口罩
                if (isHaveMask)
                {
                    //没有佩戴好口罩，但有口罩

                    NPCManager.instance.GenerateNPCDialogBox(DialogBox[0]);
                }
            }

            NPCManager.instance.AskModeSwitch();
        }
        else
        {
            //如果不在闻讯模式下，点击NPC口罩，NPC会摘口罩
            rightMask.GetComponent<Animator>().SetTrigger("TakeOffMask");
        }
    }

    public void CloseMessageBox()
    {
        UIManager.instance.MessageBox.SetActive(false);
    }

    public void SetNPC2Suspect()
    {
        NPCManager.instance.clickablePartOnSmallTableProps[0].GetComponent<ClickablePart>().isSuspect = true;
        NPCManager.instance.clickablePartOnSmallTableProps[0].GetComponent<ClickablePart>().DialogGroup = DialogBox[0];
        //TODO: 设置Rulebook相关条目为可质疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(2).GetComponent<ClickablePart>()
            .isSuspect = true;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(2).GetComponent<ClickablePart>()
            .DialogGroup = DialogBox[0];

    }

    public void ResetSuspectWhenNPC2Leave()
    {
        NPCManager.instance.clickablePartOnSmallTableProps[0].GetComponent<ClickablePart>().isSuspect = false;
        NPCManager.instance.clickablePartOnSmallTableProps[0].GetComponent<ClickablePart>().DialogGroup = null;
        //TODO: 设置Rulebook相关条目为不可置疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(2).GetComponent<ClickablePart>()
            .isSuspect = false;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(2).GetComponent<ClickablePart>()
            .DialogGroup = null;
    }

    public void SetNPC4Suspect()
    {
        //TODO: 设置Rulebook相关条目为可质疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(1).GetComponent<ClickablePart>()
            .isSuspect = true;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(1).GetComponent<ClickablePart>()
            .DialogGroup = DialogBox[0];
        Debug.Log("NPC4 set suspect!");

    }

    public void ResetSuspectWhenNPC4Leave()
    {

        //TODO: 设置Rulebook相关条目为不可置疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(1).GetComponent<ClickablePart>()
            .isSuspect = false;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(1).GetComponent<ClickablePart>()
            .DialogGroup = null;
    }

    public void SetNPC5Suspect()
    {
        //TODO: 设置Rulebook相关条目为可质疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .isSuspect = true;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .DialogGroup = DialogBox[0];

    }

    public void ResetSuspectWhenNPC5Leave()
    {
        //TODO: 设置Rulebook相关条目为不可置疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .isSuspect = false;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .DialogGroup = null;
    }

    public void SetNPC7Suspect()
    {
        //TODO: 设置Rulebook相关条目为可质疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .isSuspect = true;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .DialogGroup = DialogBox[0];
        Debug.Log("NPC7 set suspect!");

    }

    public void ResetSuspectWhenNPC7Leave()
    {
        //TODO: 设置Rulebook相关条目为不可置疑
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .isSuspect = false;
        RuleBookManager.instance.clickablePartsGroupOnRuleBook[0].transform.GetChild(3).GetComponent<ClickablePart>()
            .DialogGroup = null;
    }

    //NPC specific function
    public void NPC4PutOnMask()
     {
            wrongMask.SetActive(false);
            rightMask.SetActive(true);
            isWearMaskCorrectly = true;
            CanAccess = true;
            //rightMask.GetComponent<Animator>().SetTrigger("WearMask");
      }
    


}


